# https://leetcode.cn/problems/longest-substring-without-repeating-characters/
# 3. 无重复字符的最长子串
# 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。


class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if len(s) == 0:
            return 0

        window_begin = 0
        window_end = 0
        window_chars = set()
        longest_len = 1
        for idx in range(0, len(s)):
            current_char = s[idx]

            # 如果当前window已经包含了指定字符，则向后移动至第一个char出现的地方
            if current_char in window_chars:
                while window_begin <= window_end:
                    window_chars.remove(s[window_begin])
                    if s[window_begin] == current_char:
                        window_begin = window_begin + 1
                        break
                    window_begin = window_begin + 1

            window_end = idx
            window_chars.add(current_char)
            if window_end - window_begin + 1 > longest_len:
                longest_len = window_end - window_begin + 1

        return longest_len
